//
//  ViewController.swift
//  MoyoungCommon
//
//  Created by 李然 on 2022/7/19.
//

import UIKit
import SideMenu

open class ViewController: UIViewController {

    open override func viewDidLoad() {
        super.viewDidLoad()
        makeUI()
        
        bindViewModel()
    }
    
    deinit {
        log.info("\(self) 恭喜你，销毁了")
    }
    
    open override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    open override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        updateUI()
    }
    
    open override var preferredStatusBarStyle: UIStatusBarStyle {
        return .default
    }
    
    @objc open func backAction() {
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc open func dismissAction(complete: (() -> Void)? = nil) {
        self.dismiss(animated: true, completion: complete)
    }
    
    open func makeUI() {
        self.view.backgroundColor = R.color.common_2_bg_page()
        
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

    open func updateUI() {
        
    }
    
    open func bindViewModel() {
        
    }
    
    public func showSettingMenu() {
    
        var settings = SideMenuSettings()
        let presentationStyle : SideMenuPresentationStyle = .menuSlideIn
        presentationStyle.presentingEndAlpha = 0.5
        settings.presentationStyle = presentationStyle
        settings.menuWidth = 300
        
        let settingVC = SettingHomeViewController()
        let menu = SideMenuNavigationController(rootViewController: settingVC)
        SideMenuManager.default.leftMenuNavigationController = menu
        SideMenuManager.default.leftMenuNavigationController?.settings = settings
        RootViewManager.default.rootViewController.present(menu, animated: true)
    }
}

extension ViewController: UIGestureRecognizerDelegate {
    
    /// 返回true表示所有相同类型的手势辨认都会得到处理
    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer:
        UIGestureRecognizer) -> Bool {
        return true
    }
    
    /// 是否允许手势
    open func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if gestureRecognizer == self.navigationController?.interactivePopGestureRecognizer {
            // 只有二级以及以下的页面允许手势返回
            return (self.navigationController?.viewControllers.count)! > 1
        }
        return true
    }
}
